<template>
  <view>
    <web-view :src="src"></web-view>
  </view>
</template>
<script setup>
import { onLoad, onShareAppMessage } from "@dcloudio/uni-app";
import { ref } from "vue";
import { ossImg, oss2 } from "@/utils/utils";

import { userLogin } from "@/services/utils";
const src = ref("");
onLoad(async (options) => {
  console.log(options);
  getApp().accessLogAdd();
  if (!uni.getStorageSync("TOKEN")) {
    // 静默拿取微信用户的 openId 与 unionId 信息
    uni.login({
      provider: "weixin",
      success: async (res) => {
        console.log(res);
        const loginParam = {
          type: 1,
        };
        loginParam.code = res.code;
        const { data } = await userLogin(loginParam);
        uni.setStorageSync("TOKEN", data?.accessToken);
        uni.setStorageSync("OPENID", data?.openId);
        uni.setStorageSync("UNIONID", data?.unionId);
        getApp().emdlz2("set", "user", "openid", data?.openId);
        getApp().emdlz2("set", "user", "unionId", data?.unionId);
        getApp().sr().track("login_wxapp", {
          // more...
        });
        if (options.winningId) {
          src.value = `https://oreospring.vims04.com?from=mdm:banner;src:wxprogram&accesstoken=${data?.accessToken}&winningId=${options.winningId}&activityId=${options.activityId}&rewardId=${options.rewardId}`;
        } else {
          if (uni.getLaunchOptionsSync().query.hasOwnProperty("from")) {
            src.value = `https://oreospring.vims04.com?from=${
              uni.getLaunchOptionsSync().query.from
            }&accesstoken=${data?.accessToken}`;
          } else {
            src.value = `https://oreospring.vims04.com?from=mdm:banner;src:wxprogram&accesstoken=${data?.accessToken}`;
          }
        }

        getApp().sr().track("login_wxapp", {
          // more...
        });
      },
    });
  } else {
    if (options.winningId) {
      src.value = `https://oreospring.vims04.com?from=mdm:banner;src:wxprogram&accesstoken=${uni.getStorageSync(
        "TOKEN"
      )}&winningId=${options.winningId}&activityId=${
        options.activityId
      }&rewardId=${options.rewardId}`;
    } else {
      if (uni.getLaunchOptionsSync().query.hasOwnProperty("from")) {
        src.value = `https://oreospring.vims04.com?from=${
          uni.getLaunchOptionsSync().query.from
        }&accesstoken=${uni.getStorageSync("TOKEN")}`;
      } else {
        src.value = `https://oreospring.vims04.com?from=mdm:banner;src:wxprogram&accesstoken=${uni.getStorageSync(
          "TOKEN"
        )}`;
      }
    }
  }

  console.log(src.value);
});

onShareAppMessage((e) => {
  const pages = getCurrentPages();
  const currentPage = pages[pages.length - 1];
  console.log(currentPage);
  const pageUrl = currentPage.$page.fullPath;

  const shareObj = {
    title: "带着奥利奥去出游吧！", // 默认是小程序的名称(可以写slogan等)
    path: pageUrl + `?from=mdm:sharecard;src:shareback`, // 默认是当前页面，必须是以‘/’开头的完整路径
    imageUrl: oss2("others/ChunAo/CA_share.png"), //自定义图片路径，可以是本地文件路径、代码包文件路径或者网络图片路径，支持PNG及JPG，不传入 imageUrl 则使用默认截图。显示图片长宽比是 5:4
    success: (res) => {
      // 转发成功之后的回调
      if (res.errMsg == "shareAppMessage:ok") {
      }
    },
    fail: (err) => {
      // 转发失败之后的回调
      console.log("转发失败之后的回调", err);
      if (res.errMsg == "shareAppMessage:fail cancel") {
        // 用户取消转发
        console.log("用户取消转发");
      } else if (res.errMsg == "shareAppMessage:fail") {
        // 转发失败，其中 detail message 为详细失败信息
        console.log("转发失败，其中detail message 为详细失败信息");
      }
    },
    complete: () => {
      // 转发结束之后的回调（转发成不成功都会执行）
      console.log("转发结束之后的回调（转发成不成功都会执行）");
    },
  };
  console.log(shareObj);
  return shareObj;
});
</script>

<style></style>
